package fr.unice.polytech.chord.samples;

import java.net.UnknownHostException;

import org.objectweb.proactive.ActiveObjectCreationException;
import org.objectweb.proactive.api.PAActiveObject;
import org.objectweb.proactive.core.node.NodeException;

import fr.unice.polytech.chord.core.ChordFactory;
import fr.unice.polytech.chord.core.Tracker;

/**
 * Launch a new {@link Tracker}.
 * 
 * @author Anthony Parisy
 * @author Laurent Pellegrino
 * @author Marc Valdener
 * 
 * @version 10/30/2009
 */
public class TrackerLauncher {

	private String trackerName;

	private Tracker instance;

	public TrackerLauncher(String name) {
		super();
		this.trackerName = name;
	}

	public boolean createTracker() {
		try {
			this.instance = ChordFactory.newActiveTracker();
		} catch (ActiveObjectCreationException e) {
			e.printStackTrace();
		} catch (NodeException e) {
			e.printStackTrace();
		}
		String bindName = null;
		try {
			bindName = PAActiveObject.registerByName(this.instance,
					this.trackerName);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

		System.out.println("Tracker launched at :");
		System.out.println(bindName);

		return true;
	}

	public static void main(String[] args) throws NodeException,
			UnknownHostException {
		if (args.length != 1) {
			System.err
					.println("Usage : "
							+ TrackerLauncher.class.getCanonicalName()
							+ " trackerName");
			System.exit(1);
		}
		TrackerLauncher trackerLauncher = new TrackerLauncher(args[0]);
		trackerLauncher.createTracker();
	}

}